#!/usr/bin/env python
# coding=utf-8
import pretty_errors
import requests

# 3.2.2 高级用法
# 1.文件上传
files = {'file': open('favicon.ico', 'rb')}
r = requests.post('http://127.0.0.1:5000/post', files=files)
print(r.text)

# 2.cookies
r = requests.get('https://www.baidu.com')
print('\n cookies')
print(r.cookies)
print('cookies键-值')
for key, value in r.cookies.items():
    print(key + '=' + value)

# 模拟登录
print('\n 模拟登录--登陆前')
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'
}
r = requests.get('https://www.zhihu.com', headers=headers)
print(r.text)

headcook = {
    'cookie': '_xsrf=CBMaNBdatdcT36yC8rQjNYqBvkF7QL3Y; _zap=403eadc0-abe9-4888-8a2d-4f9e3c6b9a47; d_c0="ABBhgS--dxCPThOsB-uo0Fo0AnlrAjHDxC8=|1575693717"; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1581160716,1581661698,1581832702,1581916161; l_n_c=1; r_cap_id="YmRmMjFkZmEwNWIwNDlkNmIwZGJhN2M3NTQ2NmQ1MjM=|1581916166|94dba510e1fca4a9392504724a6a8d68d02173c5"; cap_id="NjA2ZTRkMThiYTU5NDNkZTg5YTU3YjY4YzdkMzhiMDQ=|1581916166|ec071e0fea46047cbe834ad4d37d7b57087ed61d"; l_cap_id="NmY0NWI5OGYzMzg5NDA1NGIwYjg5MzIwZDhmZmY2OTU=|1581916166|80818a75cf1aa6e1b77cbf496bce0973c749b755"; n_c=1; atoken=30_GQaYgMTTT0k2_MYRgQCJ7MbW_xxF2FxXzPN-fQ3o11JwBQKIeuhDu_Tz3Uv8W5RGHXKjnasg0sfsfyq_4Ctfc6sHecmJlD7DQHbcR273-sg; atoken_expired_in=7200; client_id="bzNwMi1qdUN0MTF3UXpzVm43Z0RIREVSS2RjNA==|1581916189|9ea663db3967b368909fe274b25cdd6ab5db7ef3"; capsion_ticket="2|1:0|10:1581916235|14:capsion_ticket|44:MTI3ZmM4MTUwMzdiNDExYjk4NGNlZWMyNjA2OGQ0ZDc=|b9f4200f2c85050db20d1ab278d7019900b941e6e1fcdface62fdeb24f2b2a94"; z_c0="2|1:0|10:1581916288|4:z_c0|92:Mi4xb1V3NEdRQUFBQUFBRUdHQkw3NTNFQ1lBQUFCZ0FsVk5nRzQzWHdDSmJhYU1LTWRhNG9tOGxoZmpPVmhnSzFEMVJn|7067a8374572f4d0b39146a1ab2dc44c269e69915fa288ccf5704fde3b4117b0"; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1581916289; tst=r; KLBRSID=b33d76655747159914ef8c32323d16fd|1581916314|1581916160'
    , 'host': 'www.zhihu.com',
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'
}
r2 = requests.get('https://www.zhihu.com', headers=headcook)
print('\n使用cookies后')  # 注意cookies的值头部不要含有空格
print(r2.text)

# 3.回话维持
requests.get('http://127.0.0.1:5000/cookies/set/number/123456789')
r = requests.get('http://127.0.0.1:5000/cookies')
print('\n 会话维持,cook无法保存传入值')
print(r.text)
s = requests.Session()
s.get('http://localhost:5000/cookies/set/number/123456789')
r = s.get('http://localhost:5000/cookies')
print('\n 使用session对象保存')
print(r.text)

# 4.SSL安全证书(测试不完全)
response = requests.get('https://www.12306.cn')
print(response.status_code)

# 余下部分忽略
